﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class UserControls_LabelControl_UlList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.NeedPage)
                BindData();
            else
                NoPagerBind();
        }
    }

    //无分页数据绑定
    protected void NoPagerBind()
    {
        this.AspNetPager1.Visible = false;

        string strTop = (this.Top == 0) ? "" : " TOP " + this.Top.ToString();
        string strWhere = (this.Where == "") ? "" : " WHERE " + this.Where;
        string sql = "SELECT " + strTop + " " + this.Select + " FROM " + this.From + strWhere + " ORDER BY " + this.Order;

        DataSet ds = Whir.SQLServerDAL.DbHelperSQL.Query(sql);

        this.Repeater1.DataSource = ds.Tables[0].DefaultView;
        this.Repeater1.DataBind();
    }

    //带分页数据绑定
    protected void BindData()
    {
        this.AspNetPager1.Visible = true;
        this.AspNetPager1.PageSize = this.PPageSize;
        if (this.PClass != "")
            this.AspNetPager1.CssClass = this.PClass;

        string PrimaryKey = this.Order.ToUpper().Replace("ASC", "").Replace("DESC", "").Trim();

        DataSet ds = Whir.SQLServerDAL.DbHelperSQL.GetList(this.From, PrimaryKey, this.Select, this.PPageSize, this.AspNetPager1.CurrentPageIndex - 1, this.Where, "", this.Order);
        this.AspNetPager1.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());

        this.Repeater1.DataSource = ds.Tables[0].DefaultView;
        this.Repeater1.DataBind();
    }

    //行绑定事件
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string content = this.Content;
        string[] fields = Whir.Label.LabelHelper.GetField(content);    //取字段标签

        //循环解析
        foreach (string field in fields)
        {
            string field2 = field.Substring(2, field.Length - 3).Trim();    //取原字段名

            try
            {
                content = content.Replace(field, DataBinder.Eval(e.Item.DataItem, field2).ToString());    //替换源数据
            }
            catch{}
        }

        //样式
        string c = (this.LiClass == "") ? "" : " class=\"" + this.LiClass + "\"";
        string s = (this.LiStyle == "") ? "" : " style=\"" + this.LiStyle + "\"";

        Literal l = e.Item.FindControl("l1") as Literal;
        l.Text = "<li" + c + s + ">" + content + "</li>";
    }

    //分页
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    //样式设置
    protected string getclass()
    {
        string c = (this.Class == "") ? "" : " class=\"" + this.Class + "\"";
        string s = (this.Style == "") ? "" : " style=\"" + this.Style + "\"";

        return c + s;
    }

    #region 属性
    /// <summary>
    /// 样式名
    /// </summary>
    private string _class = "";
    public string Class
    {
        get { return _class; }
        set { _class = value; }
    }

    /// <summary>
    /// 样式设置
    /// </summary>
    private string _style = "";
    public string Style
    {
        get { return _style;}
        set { _style = value; }
    }

    /// <summary>
    /// li样式名
    /// </summary>
    private string _liclass = "";
    public string LiClass
    {
        get { return _liclass;}
        set { _liclass = value; }
    }

    /// <summary>
    /// li样式设置
    /// </summary>
    private string _listyle = "";
    public string LiStyle
    {
        get {return _listyle; }
        set { _listyle = value; }
    }

    /// <summary>
    /// 是否分页
    /// </summary>
    private bool _needpage = false;
    public bool NeedPage
    {
        get { return _needpage;}
        set { _needpage = value; }
    }

    /// <summary>
    /// 读取记录数
    /// </summary>
    private int _top = 0;
    public int Top
    {
        get { return _top;}
        set { _top = value; }
    }

    /// <summary>
    /// 数据字段
    /// </summary>
    private string _select = "*";
    public string Select
    {
        get { return _select; }
        set { _select = value; }
    }

    /// <summary>
    /// 表名
    /// </summary>
    private string _from;
    public string From
    {
        get { return _from;}
        set { _from = value; }
    }

    /// <summary>
    /// 数据条件
    /// </summary>
    private string _where = "";
    public string Where
    {
        get { return _where; }
        set { _where = value; }
    }

    /// <summary>
    /// 数据排序
    /// </summary>
    private string _order;
    public string Order
    {
        get { return _order; }
        set { _order = value; }
    }

    /// <summary>
    /// 内容
    /// </summary>
    private string _content = "";
    public string Content
    {
        get { return _content; }
        set { _content = value; }
    }

    /// <summary>
    /// 样式
    /// </summary>
    private string _pclass = "";
    public string PClass
    {
        get { return _pclass; }
        set { _pclass = value; }
    }

    /// <summary>
    /// 每页显示记录数
    /// </summary>
    private int _ppagesize = 15;
    public int PPageSize
    {
        get { return _ppagesize; }
        set { _ppagesize = value; }
    }
    #endregion
}
